/*
 * Copyright 2009-2010 Belmont Software Services
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.belmont.backup.client;

import java.util.*;
import com.google.gwt.core.client.*;
import com.google.gwt.user.client.*;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.rpc.*;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.logical.shared.*;
import com.google.gwt.http.client.*;

public class LogsPanel extends UIPanel {
    static final int SERVER_LOG_INDEX = 0;
    static final int HOST_LOG_INDEX = 1;

    DeckPanel logsDeck = new DeckPanel();
    TextArea serverLogBox = new TextArea();
    ListBox logHostsList = new ListBox();
    TextArea hostLogBox = new TextArea();
    RequestBuilder serverLogReq = new RequestBuilder(RequestBuilder.GET, "/backup/serverlog");

    public LogsPanel(BackupUI main) {
        super(main, "Logs");
    }

    public void refresh() {
        refreshServerLog();
    }

    public Widget init() {
        DockPanel dp = new DockPanel();
        HTML help = new HTML("Shows logging information for the service and individual hosts.");
        MenuBar menu = new MenuBar(true);

        createServerLogsPanel("Show server logs", menu);
        //menu.addSeparator();
        //createHostLogsPanel("Show host logs:", menu);

        createTaskLayout("Log Tasks:", help, menu, dp, logsDeck, false, false);

        return dp;
    }

    void createServerLogsPanel(String menuTitle, MenuBar menu) {
        VerticalPanel vp = new VerticalPanel();
        HTML help = new HTML("Shows logging for the backup service.");

        help.setStyleName("backup-Help-Task");
        vp.add(help);

        serverLogBox.setCharacterWidth(120);
        serverLogBox.setHeight("400px");
        serverLogBox.setReadOnly(true);
        vp.add(serverLogBox);
        vp.setSpacing(4);
        Button refresh = new Button("Refresh");
        refresh.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent evt) {
                    refreshServerLog();
                }
            });

        vp.add(refresh);
        vp.setWidth("100%");

        logsDeck.add(vp);
	
        menu.addItem(menuTitle, true, new Command() {
                public void execute() {
                    logsDeck.showWidget(SERVER_LOG_INDEX);
                }
            });
    }

    void createHostLogsPanel(String menuTitle, MenuBar menu) {
        VerticalPanel vp = new VerticalPanel();
        HTML help = new HTML("Shows logging for a given host.");

        help.setStyleName("backup-Help-Task");
        vp.add(help);

        HorizontalPanel hp = new HorizontalPanel();

        VerticalPanel hostsPanel = new VerticalPanel();
        hostsPanel.add(new Label("Host:"));
        logHostsList.setVisibleItemCount(20);
        logHostsList.setWidth("100px");
        hostsPanel.add(logHostsList);
        hp.add(hostsPanel);

        VerticalPanel backupsPanel = new VerticalPanel();
        backupsPanel.add(new Label("Log:"));
        hostLogBox.setCharacterWidth(120);
        hostLogBox.setHeight("400px");
        hostLogBox.setReadOnly(true);
        backupsPanel.add(hostLogBox);
        hp.add(backupsPanel);

        hp.setSpacing(4);
        vp.setSpacing(4);
        vp.add(hp);
        vp.setWidth("100%");
        vp.setHeight("100%");

        logsDeck.add(vp);

        menu.addItem(menuTitle, true, new Command() {
                public void execute() {
                    logsDeck.showWidget(HOST_LOG_INDEX);
                }
            });
    }

    void refreshServerLog() {
        try {
            serverLogReq.sendRequest(null, new RequestCallback() {
                    public void onError(Request request, Throwable exception) {
                        main.print("Error getting server log: "+exception.toString());
                    }
                    public void onResponseReceived(Request request, Response response) {
                        if (response.getStatusCode() != Response.SC_OK) {
                            main.print("HTTP error getting server log: "+response.getStatusCode()+" "+response.getStatusText());
                        } else {
                            String text = response.getText();
                            serverLogBox.setText(text);
                            serverLogBox.setCursorPos(text.length() - 1);
                        }
                    }
                });
        } catch (RequestException ex) {
            main.print("Error getting server log: "+ex.toString());
        }
    }
}
